package com.dgut.Utils;

import com.dgut.model.UserInfo;
import com.dgut.service.UserService;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author GooRay
 * 创建于 2020/6/21
 */
@Component
public class PDFUtils {

    public static PDFUtils pdfUtils;

    @Autowired
    private UserService userService;

    @PostConstruct
    public void init(){
        pdfUtils = this;
        userService = this.userService;
    }

    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;

    private static int maxWidth = 520;


    static {
        try{
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font( bfChinese,16, Font.BOLD);
            headfont = new Font( bfChinese,14, Font.BOLD);
            keyfont = new Font( bfChinese,10, Font.BOLD);
            textfont = new Font (bfChinese,10, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 生成PDF文件
    public void generatePDF(Document document) throws Exception {

        // 段落
        Paragraph paragraph = new Paragraph("用户信息", titlefont);
        paragraph.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setIndentationLeft(12); //设置左缩进
        paragraph.setIndentationRight(12); //设置右缩进
        paragraph.setFirstLineIndent(24); //设置首行缩进
        paragraph.setLeading(20f); //行间距
        paragraph.setSpacingBefore(5f); //设置段落上空白
        paragraph.setSpacingAfter(10f); //设置段落下空白

        // 直线
        Paragraph p1 = new Paragraph();
        p1.add(new Chunk(new LineSeparator()));


        // 表格
        PdfPTable table = createTable(new float[] { 80, 140, 40, 40, 100, 50 });
        table.addCell(createCell("用户名", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("身份证", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("栋号", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("房号", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("最新出入时间", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("温度", keyfont, Element.ALIGN_CENTER));
        Integer totalQuantity = 0;
        List<UserInfo> userInfos = pdfUtils.userService.findAllMessage();
        for (UserInfo userInfo : userInfos) {
            table.addCell(createCell(userInfo.getUser_name(), textfont));
            table.addCell(createCell(userInfo.getUser_id_card(), textfont));
            table.addCell(createCell(userInfo.getBuilding().toString(), textfont));
            table.addCell(createCell(userInfo.getRoom_number().toString(), textfont));
            table.addCell(createCell(userInfo.getLastest_time().toString(), textfont));
            table.addCell(createCell(userInfo.getLastest_temp(), textfont));
            totalQuantity ++;
        }
        table.addCell(createCell("总计", keyfont));
        table.addCell(createCell("", textfont));
        table.addCell(createCell("", textfont));
        table.addCell(createCell("", textfont));
        table.addCell(createCell(String.valueOf(totalQuantity) + "条记录", textfont));
        table.addCell(createCell("", textfont));

        document.add(paragraph);
        document.add(p1);
        document.add(table);
    }

    public PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }


    public static String DownLoad(){
        File file = new File("demo.pdf");
        try {
            // 1.新建document对象
            Document document = new Document(PageSize.A4);// 建立一个Document对象

            // 2.建立一个书写器(Writer)与document对象关联
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            file.getName();
            // 3.打开文档
            document.open();

            // 4.向文档中添加内容
            new PDFUtils().generatePDF(document);

            // 5.关闭文档
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getName();
    }

}
